﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AgNet.Models;
using RestSharp;

namespace AgNet.Helpers
{
    public class RequestHelper
    {
        private string version;

        public RequestHelper(string version)
        {
            this.version = version;
        }

        public RestRequest CreateLoginRequest(string appId, string appSecret, string userMail, string userPassword)
        {
            var request = CreateRequest(Method.POST, Requests.token, this.version);

            request.AddParameter("client_id", appId);
            request.AddParameter("client_secret", appSecret);
            request.AddParameter("user_email", userMail);
            request.AddParameter("user_password", userPassword);
            request.AddParameter("grant_type", "password");

            return request;
        }

        public RestRequest CreateLoginRequest(string appId, string appSecret, string refreshToken)
        {
            var request = CreateRequest(Method.POST, Requests.token, this.version);

            request.AddParameter("client_id", appId);
            request.AddParameter("client_secret", appSecret);
            request.AddParameter("refresh_token", refreshToken);

            return request;
        }

        public RestRequest CreateUserInfoRequest(string clientId, string accessToken)
        {
            var request = CreateRequest(Method.GET, Requests.state, this.version);

            request.AddParameter("client_id", clientId);
            request.AddParameter("access_token", accessToken);

            return request;
        }

        /// <summary>
        /// Fetch the list of user's playlists, artists, albums or genres
        /// </summary>
        /// <param name="type">playlist|artist|genre.</param>
        /// <param name="resourceId">the id of the resource you wish to fetch.</param>
        /// <param name="imgSize">small|medium|large.</param>
        /// <param name="clientId">your application's App ID.</param>
        /// <param name="accessToken">a valid session token.</param>
        /// <returns>The RestRequest object.</returns>
        public RestRequest FetchResourceListRequest(ResourceTypes type, int offset, ImageSizes imgSize, string clientId, string accessToken)
        {
            var request = CreateRequest(Method.GET, Requests.resourceList, this.version);
            
            request.AddParameter("type", type);
            
            request.AddParameter("offset", offset);
            
            request.AddParameter("img_size", imgSize);

            request.AddParameter("client_id", clientId);

            request.AddParameter("access_token", accessToken);

            return request;
        }

        /// <summary>
        /// Fetch the list of songs by playlist, artist, genre.
        /// </summary>
        /// <param name="type">playlist|artist|genre.</param>
        /// <param name="resourceId">the id of the resource you wish to fetch.</param>
        /// <param name="offset">0-based offset to begin retrieving from.</param>
        /// <param name="clientId">your application's App ID.</param>
        /// <param name="accessToken">a valid session token.</param>
        /// <returns>The RestRequest object.</returns>
        public RestRequest FetchSongsListRequest(ResourceTypes type, string resourceId, int offset, string clientId, string accessToken)
        {
            var request = CreateRequest(Method.GET, Requests.songList, this.version);

            request.AddParameter("type", type);

            request.AddParameter("resource_id", resourceId);

            request.AddParameter("offset", offset);

            request.AddParameter("client_id", clientId);

            request.AddParameter("access_token", accessToken);

            return request;
        }

        /// <summary>
        /// Fetch album(s), along with a list of songs in each album
        /// </summary>
        /// <param name="resourceId">the id of the resource you wish to fetch.</param>
        /// <param name="imgSize">small|medium|large (for album-level art).</param>
        /// <param name="offset">0-based offset to begin retrieving from.</param>
        /// <param name="clientId">your application's App ID.</param>
        /// <param name="accessToken">a valid session token.</param>
        /// <returns>The RestRequest object.</returns>
        public RestRequest FetchSongsListRequest(string resourceId, ImageSizes imgSize, int offset, string clientId, string accessToken)
        {
            var request = CreateRequest(Method.GET, Requests.songList, this.version);

            request.AddParameter("type", ResourceTypes.album);

            request.AddParameter("resource_id", resourceId);

            request.AddParameter("img_size", imgSize);

            request.AddParameter("offset", offset);

            request.AddParameter("client_id", clientId);

            request.AddParameter("access_token", accessToken);

            return request;
        }

        public RestRequest FetchAudio(string audioToken, string encoding, string quality, string fetchReason, string clientId, string accessToken)
        {
            var request = CreateRequest(Method.GET, Requests.audio, this.version);

            request.AddParameter("audio_token", audioToken);

            request.AddParameter("encoding", encoding);

            request.AddParameter("quality", quality);

            request.AddParameter("fetch_reason", fetchReason);

            request.AddParameter("client_id", clientId);

            request.AddParameter("access_token", accessToken);

            return request;
        }

        private static RestRequest CreateRequest(Method method, Requests requestName, string version)
        {
            var newRequest = new RestRequest(method);

            newRequest.Resource = "{version}/{reqName}";
            newRequest.AddParameter("reqName", requestName, ParameterType.UrlSegment);
            newRequest.AddParameter("version", version, ParameterType.UrlSegment);

            return newRequest;
        }
    }
}
